from flask import Blueprint, jsonify, make_response, request
from flask_restful import Resource, Api, reqparse
from models.model import *

course_bp = Blueprint('course_bp', __name__, url_prefix='/')

api = Api(course_bp)

class CourseOne(Resource):
    def get(self, id):
        course_info = Course.query.filter(Course.id == id).one()
        if not course_info:
            return jsonify({
                'code':408,
                'msg':'无课程'
            })
        courseteacher = Course2Teacher.query.filter(Course2Teacher.courses_id == course_info.id).all()
        teacher_list = []
        for i in courseteacher:
            a = i.teachers_id
            teacher = Teacher.query.filter(Teacher.id == a).one()
            b = {
                'id': teacher.id,
                'name': teacher.name,
                'attention_num': teacher.attention_num,
                'info': teacher.info,
                'status': teacher.status,
                'photo': teacher.photo
            }
            teacher_list.append(b)
        print(course_info.money)
        course_list = {
            'course_id':course_info.id,
            'name':course_info.name,
            'info':course_info.info,
            'total':course_info.total,
            'total_time':course_info.total_time,
            'photo':course_info.photo,
            'money':float(course_info.money),
            'teacher_num':course_info.teacher_num
        }

        return jsonify({
            'code':200,
            'msg':'获取成功',
            'data':{
            'course_list':course_list,
            'teacher_list':teacher_list}
        })

    def put(self, id):
        req = reqparse.RequestParser()
        req.add_argument('cupon_id')
        args = req.parse_args()
        cupon_id = args['cupon_id']
        student_id = request.cookies.get("student_id")

        course_info = Course.query.filter(Course.id == id).one()
        if not course_info:
            return jsonify({
                'code': 408,
                'msg': '无课程'
            })
        student_info = Student.query.filter(Student.id == student_id).one()
        # todo 获取是否有优惠卷
        # todo 课程价格减去优惠卷值
        # todo 判断
        cupon_money = 0
        real_price = float(course_info.money) - float(cupon_money)

        if not float(student_info.money)>= real_price:
            return jsonify({
                'code':444,
                'msg':'余额不足'
            })
        # 余额减少
        student_money = float(student_info.money) - float(real_price)
        student_info.money = student_money
        # 学生特色课和一对一
        if course_info.type == 1:
            student_info.course_num += 1
        else:
            student_info.solo_num += 1

        # 课程购买数量
        course_info.buy_num += 1

        # 添加订单
        # order = {
        #     "courses_name": course_info.name,
        #     "courses_id": course_info.name,
        #     "student_id": student_id,
        #     "real_price":real_price,
        #     "price": course_info.money,
        #     "fail": 0,
        #     "coupon_id": cupon_id,
        # }
        order_info = ClassOrder(courses_name=course_info.name, courses_id=course_info.id, student_id=student_id,
                                real_price=real_price, price=float(course_info.money), fail=0, coupon_id=1)
        db.session.add(order_info)
        db.session.commit()

    # def put(self, id):
    #     Course.query.filter(Course.id == 1).update({
    #         'money':3600.99
    #     })
    #     db.session.commit()
    #     return '测试成功'


api.add_resource(CourseOne, 'get_course/<int:id>')

